package cc.fs.zhihuapp.ui.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.App;
import cc.fs.zhihuapp.AppConfig;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;
import cc.fs.zhihuapp.ui.fragment.DataFragment;
import cc.fs.zhihuapp.utils.PxUtils;

/**
 * Created by fostion on 2015/9/7.
 */
public class MainActivity extends BaseActivity {

    PopupWindow window;
    View moreView;
    DataFragment fragment;
    String className;
    TextView styleText;
    RadioGroup radioGroup;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.rootView)
    RelativeLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_base);
        ButterKnife.inject(this);

        setUpToolbar();
        setUpMoreView();
        setUpFragment();
    }

    void setUpToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("我的知乎日报");
        setSupportActionBar(toolbar);
    }

    void setUpFragment() {
        className = DataFragment.class.getName();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragment = (DataFragment) fragmentManager.findFragmentByTag(className);
        if (fragment == null || fragment.isDetached()) {
            fragment = (DataFragment) Fragment.instantiate(this, className);
            fragmentTransaction.add(R.id.fragment_contain, fragment, className);
        }
        fragmentTransaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_my_activity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (fragment.optionsItemSelected(item)) {
            return true;
        } else if (item.getItemId() == R.id.action_switch_theme) {
            if (window.isShowing()) {
                window.dismiss();
            } else {
                window.showAsDropDown(findViewById(R.id.window_layout), 0, 0);
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
        fragment.syncState();
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    void setUpMoreView() {
        moreView = getLayoutInflater().inflate(R.layout.window_main, null);
        styleText = (TextView) moreView.findViewById(R.id.action_style_name);
        radioGroup = (RadioGroup) moreView.findViewById(R.id.radioGroup);
        if (getCurrentThemeStyle().equals(AppConfig.VALUE_NIGHT)) {
            styleText.setText("白天模式");
        } else {
            styleText.setText("夜间模式");
        }

        styleText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getCurrentThemeStyle().equals(AppConfig.VALUE_NIGHT)) {
                    window.dismiss();
                    setTheme(R.style.AppTheme);
                    AppConfig.save(AppConfig.KEY_THEME, AppConfig.VALUE_LIGHT);
                    reload();
                } else {
                    window.dismiss();
                    setTheme(R.style.AppTheme_Night);
                    AppConfig.save(AppConfig.KEY_THEME, AppConfig.VALUE_NIGHT);
                    reload();
                }
            }
        });

        int index;
        if (getCurrentFontStyle().equals(AppConfig.VALUE_FONT_BIG)) {
            index = 2;
        } else if (getCurrentFontStyle().equals(AppConfig.VALUE_FONT_MEDIUM)) {
            index = 1;
        } else {
            index = 0;
        }
        ((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.font_small:
                        window.dismiss();
                        AppConfig.save(AppConfig.KEY_FONTSTYLE, AppConfig.VALUE_FONT_SMALL);
                        setThemeStyle(AppConfig.VALUE_FONT_SMALL);
                        reload();
                        break;
                    case R.id.font_medium:
                        window.dismiss();
                        AppConfig.save(AppConfig.KEY_FONTSTYLE, AppConfig.VALUE_FONT_MEDIUM);
                        setThemeStyle(AppConfig.VALUE_FONT_MEDIUM);
                        reload();
                        break;
                    case R.id.font_big:
                        window.dismiss();
                        AppConfig.save(AppConfig.KEY_FONTSTYLE, AppConfig.VALUE_FONT_BIG);
                        setThemeStyle(AppConfig.VALUE_FONT_BIG);
                        reload();
                        break;
                }
            }
        });

        window = new PopupWindow(this);
        window.setWidth(PxUtils.dip2px(this, 250));
        window.setHeight(PxUtils.dip2px(this, 120));
        window.setContentView(moreView);
        window.setOutsideTouchable(false);
        window.setBackgroundDrawable(new ColorDrawable(0));
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (window != null && window.isShowing()) {
            window.dismiss();
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void handler(Message msg) {
    }

    private long exitTime = 0;

    @Override
    public void onBackPressed() {
        if (window != null && window.isShowing()) {
            window.dismiss();
            return;
        }

        long endTime = System.currentTimeMillis();
        if ((endTime - exitTime) > 2000) {
            T("再按一次退出程序~ (¬_¬)");
            exitTime = System.currentTimeMillis();
            return;
        } else {
            finish();
            return;
        }
    }
}
